/*!
 * Component: components/Index/index
 * License: MIT
 * Created: 2019-10-22 14:31
 */

import { knowledgeApi } from '/api/konwledge'
import { mallApi } from '/api/mall'
import { userApi } from '/api/user'
import { checkAuth } from '/store/actions'
import { getServer, isLogin } from '/store/mutations'
import { GlobalData } from '/store/state'
import { $event } from '/utils'

Component({
  /**
   * Component properties
   */
  properties: {
    topic: { type: Boolean, value: false },
    theme: { type: Boolean, value: false },
    product: { type: Boolean, value: false },
    bottom: { type: String, value: '60rpx' },
    isknowledge: { type: Boolean, value: false },
    isproduct: { type: Boolean, value: false },
    istheme: { type: Boolean, value: false },
    ishome: { type: Boolean, value: false },
    search: { type: Boolean, value: false },
  },

  /**
   * Component initial data [private]
   */
  data: {
    access: GlobalData.access,
    /** 点击锁 */
    lock: false,
    canPublishKnowledge: false,
    canPublishProduct: false,
    can_publishTheme: false,
    can_activity: false,
    about: '',
    themeConfig: GlobalData.themeConfig,
    /** 显示锁 */
    showLock: false,
  },

  /**
   * lifecycle of component
   */
  lifetimes: {
    attached() {
      $event.on('after:boot', this, () => {
        this.setData({ access: GlobalData.access, themeConfig: GlobalData.themeConfig })
      })
      if (GlobalData.userAbilities.length > 0) {
        this.power()
      }
      $event.on('after:abilities', this, () => {
        this.power()
      })
      const about = this.data.topic
        ? 'topic'
        : this.data.theme
        ? 'theme'
        : this.data.product
        ? 'product'
        : ''

      this.setData({ access: GlobalData.access, themeConfig: GlobalData.themeConfig, about })
    },
  },

  /**
   * Component methods
   */
  methods: {
    async power() {
      const abilities: any = GlobalData.userAbilities
      const { data: user } = wx.getStorageSync(`${getServer()}/logged`)
      const canPublishKnowledge = this.data.isknowledge
        ? (await (await knowledgeApi.canPublishKnowledge({ user_id: user.id })).can_publish) ||
          (abilities && abilities.some((v: any) => v.name === 'knowledge:admin'))
        : false
      const canPublishProduct = this.data.isproduct
        ? await (
            await mallApi.canPublishProduct({ user_id: user.id })
          ).can_publish
        : false
      const can_activity = abilities && abilities.some((v: any) => v.name === 'event:create')
      this.setData({
        canPublishKnowledge,
        canPublishProduct,
        can_activity,
      })
      let can_publishTheme = false
      if (this.data.istheme) {
        const { config: { create_permission = '', permission = '' } = {} } =
          this.data.themeConfig || {}

        if (create_permission === 'all') {
          can_publishTheme = true
        } else if (
          create_permission === 'verification' &&
          user &&
          user.verification &&
          user.verification.state === 1
        ) {
          can_publishTheme = true
        } else if (create_permission === 'specialUser') {
          if (abilities && abilities.some((v: any) => v.name === permission)) {
            can_publishTheme = true
          }
          //  else if (abilities && abilities.some((v: any) => v.name === '[feed] Manage Theme')) {
          //   return this.setData({ can_publishTheme: true })
          // }
        }
      }
      this.setData({ can_publishTheme, showLock: true })
    },
    async showMore() {
      /** 电脑小程序劝退 */
      if (
        GlobalData.system != 'ios' &&
        GlobalData.system != 'android' &&
        GlobalData.system != 'devtools'
      ) {
        // wx.showModal({
        //   title: '提示',
        //   content: '电脑端的体验尚不完善，请您在手机上进行所有的发布操作',
        // })
        // return
      }
      if (GlobalData.userAbilities.length == 0) {
        GlobalData.userAbilities = await userApi.abilitiesList()
        $event.emit('after:abilities')
      }
      if (!this.data.access.ugc || !this.data.showLock) return
      const {
        data: { about },
      } = this
      if (this.data.lock) return
      this.setData({
        lock: true,
      })
      if (!isLogin()) {
        await checkAuth()
      }

      const actionSheets: any[] = [
        {
          name: '图片',
          url: `/pages/publish/publish?type=image&about=${about}`,
        },
      ]
      // if (GlobalData.access.video) {
      //   actionSheets.push({
      //     name: '视频',
      //     url: `/pages/publish/publish?type=video&about=${about}`,
      //   })
      // }

      // if (this.data.can_publishTheme) {
      //   actionSheets.push({
      //     name: '创建话题',
      //     url: '/pages/creat-theme/creat-theme',
      //   })
      // }
      // if (this.data.canPublishProduct) {
      //   actionSheets.push({
      //     name: '商品',
      //     url: '/mall/publish/publish',
      //   })
      // }

      // if (this.data.canPublishKnowledge) {
      //   actionSheets.push({
      //     name: '知识',
      //     url: '/index/knowledge/publish/publish',
      //   })
      // }
      // if (this.data.ishome && this.data.can_activity) {
      //   actionSheets.push({
      //     name: '活动',
      //     url: `/pages/activity/publish/publish`,
      //   })
      // }
      // ActionSheet 最多只支持6个item
      // if (this.data.ishome && actionSheets.length < 6) {
      //   actionSheets.push({
      //     name: '发布提问',
      //     url: `/index/creat-knowAbout/creat-knowAbout`,
      //   })
      // }
      this.setData({
        lock: false,
      })
      wx.showActionSheet({
        itemList: actionSheets.map((v) => v.name),
        success({ tapIndex }) {
          const action = actionSheets[tapIndex]

          if (action.url) {
            wx.navigateTo({ url: action.url })
          }
        },
      })
      this.setData({ lock: false })
    },
  },
})
